<?php
/**
 * Created by PHPStorm
 * User: xiest
 * Date: 2021/6/6
 * Email: <939369942@qq.com>
 */
namespace basic\services;

use Endroid\QrCode\QrCode;
use think\facade\Config;

class QrcodeService
{
    public function merCode($mer_id)
    {
        try {
           // $name = 'code'.date('YmdHis').substr(md5($mer_id),0,8).rand(100000,999999).'.png';
            $siteUrl = config('app.site_url');

            $url = $siteUrl.'api/mer_code/dispatch/'.$mer_id;
            $qrCode = new QrCode($url);
            $content = $qrCode->writeString();
            $qrCode->setSize(150);
            $upload = UploadService::create();
            $result= $upload->toPath(date('Ymd'))->stream($content,'png');
            if(!$result) return $upload->getError();
            return trim(Config::get('app.site_url'),"/").'/'.trim($upload->getUploadInfo()['dir'],"/");

        }catch (\Exception $e){
            return $e->getMessage();
        }
    }


    public function comUserCode($user_id)
    {
        try {
            // $name = 'code'.date('YmdHis').substr(md5($mer_id),0,8).rand(100000,999999).'.png';
            $siteUrl = config('app.site_url');

            $url = $siteUrl.'api/com_user_code/dispatch/'.$user_id;
            $qrCode = new QrCode($url);
            $content = $qrCode->writeString();
            $qrCode->setSize(150);
            $upload = UploadService::create();
            $result= $upload->toPath(date('Ymd'))->stream($content,'png');
            if(!$result) return $upload->getError();
            return trim(Config::get('app.site_url'),"/").'/'.trim($upload->getUploadInfo()['dir'],"/");

        }catch (\Exception $e){
            return $e->getMessage();
        }
    }

    public function createMerBg(string $img, string $water)
    {
        $height = 640;
        $width = 910;
        $imgRs = $this->createImage($img);
        $waterMarkRs = $this->createImage($water);
        if(!$imgRs || !$waterMarkRs) return;
        $bg = imagecreatetruecolor($height, $width);
        $x = imagesx($imgRs);
        $y = imagesy($imgRs);
        $waterX = imagesx($waterMarkRs);
        $waterY = imagesy($waterMarkRs);

        imagecopyresized($bg, $imgRs, 0, 0, 0, 0, floor($height), floor($width), $x, $y);
        imagecopyresized($bg, $waterMarkRs, 20, 600, 0, 0, 270, 270, $waterX, $waterY);

        ob_start();
        header("content-type:image/png");
        imagepng($bg);
        $content = ob_get_contents();
        ob_clean();

        header("content-type:text/html");
        $upload = UploadService::create();
        $result= $upload->toPath(date('Ymd'))->stream($content,'png');
        if(!$result) return $upload->getError();
        return trim(Config::get('app.site_url'),"/").'/'.trim($upload->getUploadInfo()['dir'],"/");
    }



    public function createImage($img){

        $ext = explode(".", $img);
        $ext = strtolower($ext[count($ext)-1]);

        if(!in_array($ext,['jpg','jpeg','png','gif'])){
            // throw new \Exception('图片类型错误');
            return null;
        }

        if($ext == "jpg" || $ext == "jpeg")
            $imgRs = imagecreatefromjpeg($img);
        elseif($ext == "png")
            $imgRs = imagecreatefrompng($img);
        elseif($ext == "gif")
            $imgRs = imagecreatefromgif($img);

        return $imgRs;
    }


}